$TITLE ('RTX-51 ISR-RECEIVE-FUNCTION')
$SYMBOLS
$NOXREF
$NOCOND
$NOMOD51
$NORB
$PAGELENGTH(80) PAGEWIDTH(110)
;************************************************************************
;*									*
;*    R T X - 5 1  :   RTX-51 ISR-RECEIVE-MESSAGE FUNCTION              *
;*							     		*
;*----------------------------------------------------------------------*
;*                                                                      *
;*    Filename     :   RTXIRECV.A51                                     *
;*    Language     :   Keil A-51                                        *
;*    Dev. system  :   Keil uVision2                                    *
;*    Targetsystem :   Any system based upon 8051 up                    *
;*                                                                      *
;*    Purpose      :   Implements the RTX-51 ISR-Functions              *
;*								     	*
;*----------------------------------------------------------------------*
;* Rev. |  Released   | Programmer    | Comments                        *
;*----------------------------------------------------------------------*
;* 0.1  |  9.7.1991   | Th. Fischler  | First Version for RTX-51 V4     *
;* 5.00 | 20.10.1994  | E. Glatz      | Release 5.00                    *
;* 7.00 | 13.11.2001  | T. Mueller    | Release 7.00                    *
;************************************************************************
;*    Copyright 1991 .. 2001 METTLER  &  FUCHS  AG,  CH-8953 Dietikon   *
;************************************************************************

$INCLUDE (RTXDEBUG.INC)
$INCLUDE (RTXIMASK.INC)

;*----------------------------------------------------------------------*
;*	I M P O R T S
;*----------------------------------------------------------------------*

$INCLUDE (RTXCONST.INC)
$INCLUDE (RTXEXTRN.INC)

;        FROM RTXCONF
EXTRN    BIT   (?RTX_TFLAG)


;*----------------------------------------------------------------------*
;*	E X P O R T S
;*----------------------------------------------------------------------*

PUBLIC   _isr_recv_message


;*----------------------------------------------------------------------*
;*	P R O C E D U R E S
;*----------------------------------------------------------------------*

;*----------------------------------------------------------------------*
;*      I S R _ R E C V _ M E S S A G E
;*----------------------------------------------------------------------*
;*      System call for receiving a message from a mailbox.
;*----------------------------------------------------------------------*
;*      Input:   - R7: Mailbox-Number
;*               - R4: HIGH(message-pointer)
;*               - R5: LOW(message-pointer)
;*
;*      Output:  - Returns execution status in R7 (OK, NOT_OK)
;*
;*----------------------------------------------------------------------*

?RTX?_isr_recv_message?RTXIRECV   SEGMENT CODE
                                  RSEG    ?RTX?_isr_recv_message?RTXIRECV

_isr_recv_message:
               DBG_SYS_ENTRY
               ; Check the mailbox-parameter
               MOV   A, R7
               SETB  C
               SUBB  A, #7H
               JC    MBX_OK2
               ; Mailbox-Nbr is not ok
               MOV   R7, #NOT_OK
               DBG_SYS_EXIT
               RET

MBX_OK2:       ; Check if messages available
               ; Get MBX#_COUNT
               MOV   A, R7
               SWAP  A
               RR    A
               ADD   A, #LOW(?RTX_MBX_CONTROL_BASE); Add base of mbx.control block
               MOV   DPL, A
               MOV   DPH, #HIGH(?RTX_MBX_CONTROL_BASE); DPTR --> MBX#_COUNT
               GLOBAL_INT_DISABLE
               MOVX  A, @DPTR
               MOV   R0, A                      ; store MBX#_COUNT

               ; Check if messages available
               DEC   A
               JNB   ACC.7, MSGS_WAIT
               ; No messages available
               GLOBAL_INT_ENABLE
               MOV   R7, #NOT_OK
               DBG_SYS_EXIT
               RET

MSGS_WAIT:     ; At least one message is available
               ; MBX#_COUNT=MBX#_COUNT-1
	       MOVX  @DPTR, A

	       ; Set up DPTR as pointer to mailbox message list
	       INC   DPL
	       INC   DPL
	       MOVX  A, @DPTR	                ; Get MBX#_FIRST
               MOV   R3, DPL                    ; save ptr to MBX#_FIRST
	       MOV   DPL, A
               MOV   DPH, #HIGH(?RTX_MBX_PAGE)

               ; R1 := HIGH (message)
	       MOVX  A, @DPTR
               MOV   R1, A
	       ; Increment FIFO pointer
	       INC   DPL
	       ANL   DPL, #0EFH
               ; R2 := LOW (message)
	       MOVX  A, @DPTR
               MOV   R2, A

	       ; Increment FIFO pointer
	       MOV   A, DPL
	       INC   A
	       ANL   A, #0EFH
               MOV   DPL, R3
               MOV   DPH, #HIGH(?RTX_MBX_CONTROL_BASE)
	       MOVX  @DPTR, A			; Store new MBX#_FIRST

               ; Mailbox variables updated, reenable the ISR-Interrupts
               GLOBAL_INT_ENABLE

	       ; Set up DPTR as pointer to message return location
               MOV   DPH, R4
               MOV   DPL, R5
	       ; Store high byte of message
               MOV   A, R1
	       MOVX  @DPTR, A
	       ; Store low byte of message
	       INC   DPTR
               MOV   A, R2
	       MOVX  @DPTR, A

               ; Check if mailbox was full before message was read
               CJNE  R0, #8, NOT_FULL

NF_CHECK_REQ:  GLOBAL_INT_DISABLE
               ; Check if free place in LIFO
               MOV   A, ?RTX_ACT_LIFO_EL
               CLR   C
               SUBB  A, #ISR_LIFO_SIZE-1
               JC    LIFO_FREE3
               GLOBAL_INT_ENABLE
               MOV   R7, #NOT_OK
               DBG_SYS_EXIT
               RET

LIFO_FREE3:    MOV   A, ?RTX_ACT_LIFO_EL
               ADD   A, #LOW(?RTX_ISR_LIFO)
               MOV   DPL, A
               MOV   DPH, #HIGH(?RTX_ISR_LIFO)
               ; Setup LIFO-entry
               MOV   A, #ISR_NF_CHK_CMD
               MOVX  @DPTR, A                   ; store Command
               INC   DPL
               INC   DPL
               MOV   A, R7
               MOVX  @DPTR, A                   ; store mailbox-number
               MOV   A, ?RTX_ACT_LIFO_EL
               ADD   A, #3
               MOV   ?RTX_ACT_LIFO_EL, A        ; ?RTX_ACT_LIFO_EL incremented

               ; Check if System-Clock-Handler is active
               JB    ?RTX_SYSCLK_ISR, CLK_ACT
               SETB  ?RTX_TFLAG                 ; activate overflow-INT
CLK_ACT:       GLOBAL_INT_ENABLE

NOT_FULL:      MOV   R7, #OK
               DBG_SYS_EXIT
               RET


;
;   END OF MODULE
;
	END


